﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditTravelGuidePrice : AdminPageBase
    {
        private TravelGuidePriceInfo _travelGuidePriceInfo = new TravelGuidePriceInfo();
        private readonly TravelGuidePriceController _travelGuidePriceController = new TravelGuidePriceController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TravelGuidePriceId"]))
                _travelGuidePriceInfo = _travelGuidePriceController.GetTravelGuidePrice(int.Parse(Request.QueryString["TravelGuidePriceId"]));

            if (Page.IsPostBack) return;

            LoadLevel();
            LoadLanguage();
            LoadSchedule();
            LoadStatus();
            LoadAdminUser();

            if (_travelGuidePriceInfo.TravelGuidePriceId != -1)
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListTravelGuidePrice.aspx?xml=TravelGuidePrice");
        }

        private void SaveData()
        {
            _travelGuidePriceInfo.TravelGuidePriceName = ddlLevel.SelectedItem.Text + " - " + ddlLanguage.SelectedItem.Text + " - " + ddlSchedule.SelectedItem.Text;
            _travelGuidePriceInfo.Level = int.Parse(ddlLevel.SelectedValue);
            _travelGuidePriceInfo.LanguageId = int.Parse(ddlLanguage.SelectedValue);
            _travelGuidePriceInfo.ScheduleId = int.Parse(ddlSchedule.SelectedValue);
            _travelGuidePriceInfo.Price = decimal.Parse(txtPrice.Text);
            _travelGuidePriceInfo.ExtraMoney = decimal.Parse(txtExtraMoney.Text);
            _travelGuidePriceInfo.Status = int.Parse(ddlStatus.SelectedValue);
            if (_travelGuidePriceInfo.TravelGuidePriceId == -1)
            {
                _travelGuidePriceInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _travelGuidePriceInfo.CreatedDate = DateTime.Now;
                _travelGuidePriceController.InsertTravelGuidePrice(_travelGuidePriceInfo);
            }
            else
            {
                _travelGuidePriceInfo.UpdatedBy = UserId;
                _travelGuidePriceInfo.UpdatedDate = DateTime.Now;
                _travelGuidePriceController.UpdateTravelGuidePrice(_travelGuidePriceInfo);
            }
            Response.Redirect("/Pages/ListTravelGuidePrice.aspx?xml=TravelGuidePrice");
        }

        private void RenderForm()
        {
            ddlLevel.SelectedValue = _travelGuidePriceInfo.Level.ToString();
            ddlLanguage.SelectedValue = _travelGuidePriceInfo.LanguageId.ToString();
            ddlSchedule.SelectedValue = _travelGuidePriceInfo.ScheduleId.ToString();
            txtPrice.Text = _travelGuidePriceInfo.Price.ToString();
            txtExtraMoney.Text = _travelGuidePriceInfo.ExtraMoney.ToString();
            ddlStatus.SelectedValue = _travelGuidePriceInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _travelGuidePriceInfo.CreatedBy.ToString();
        }

        private void LoadLevel()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Level.xml"));
            var dv = ds.Tables["Level"].DefaultView;
            ddlLevel.DataTextField = "Name";
            ddlLevel.DataValueField = "ID";
            ddlLevel.DataSource = dv;
            ddlLevel.DataBind();

            ddlLevel.Items.Insert(0, new ListItem("--- Chọn trình độ ---", "-1"));
        }
        private void LoadLanguage()
        {
            ddlLanguage.DataSource = new LanguageController().ListLanguage();
            ddlLanguage.DataTextField = "LanguageName";
            ddlLanguage.DataValueField = "LanguageId";
            ddlLanguage.DataBind();

            ddlLanguage.Items.Insert(0, new ListItem("--- Chọn ngôn ngữ ---", "-1"));
        }
        private void LoadSchedule()
        {
            ddlSchedule.DataSource = new ScheduleController().ListSchedule()
                .Where(p => p.Status == (int) Globals.DocumentStatus.Use)
                .OrderBy(p => p.ScheduleName);
            ddlSchedule.DataTextField = "ScheduleName";
            ddlSchedule.DataValueField = "ScheduleId";
            ddlSchedule.DataBind();

            ddlSchedule.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }
        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}